package com.bosssoft.trainee.nontax.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bosssoft.trainee.nontax.entity.dto.RoleDTO;
import com.bosssoft.trainee.nontax.entity.po.Role;
import com.bosssoft.trainee.nontax.entity.vo.RoleQueryVO;
import com.bosssoft.trainee.nontax.mapper.RoleMapper;
import com.bosssoft.trainee.nontax.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqiang
 * @date 2022/6/17 21:16
 */
@Service
public class RoleServiceImp extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    /**
     * 添加
     *
     * @param roleDto RoleDTO
     */
    public void saveRole(RoleDTO roleDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDto, role);
        roleMapper.insert(role);
    }

    /**
     * 修改
     *
     * @param roleDTO RoleDTO
     */
    public boolean updateRole(RoleDTO roleDTO) {
        Role role = roleMapper.selectById(roleDTO.getRoleId());
        if (role == null) {
            return false;
        } else {
            //先查询再更新，乐观锁生效
            BeanUtils.copyProperties(roleDTO, role);
            //更新数据
            roleMapper.updateById(role);
            return true;
        }
    }

    /**
     * 角色列表
     */
    public List<RoleDTO> getList() {
        List<RoleDTO> listDTO = new ArrayList<>();
        List<Role> list = this.list();
        for (Role role : list) {
            RoleDTO roleDTO = new RoleDTO();
            BeanUtils.copyProperties(role, roleDTO);
            listDTO.add(roleDTO);
        }
        return listDTO;
    }

    /**
     * 分页
     *
     * @param pageNum  pageNum
     * @param pageSize pageSize
     * @return list
     */
    public List<RoleDTO> getListDTO(Integer pageNum, Integer pageSize) {
        Page<Role> pageInfo = new Page<>(pageNum, pageSize);
        this.page(pageInfo);
        List<Role> list = pageInfo.getRecords();
        List<RoleDTO> listDTO = new ArrayList<>();
        for (Role role : list) {
            RoleDTO roleDTO = new RoleDTO();
            BeanUtils.copyProperties(role, roleDTO);
            listDTO.add(roleDTO);
        }
        return listDTO;
    }

    /**
     * 多条件+模糊查询
     *
     * @param roleQueryVO roleQueryVO
     * @return list
     */
    public List<RoleDTO> findRole(RoleQueryVO roleQueryVO) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        Integer id = roleQueryVO.getRoleId();
        String code = roleQueryVO.getRoleCode();
        String name = roleQueryVO.getRoleName();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("role_name", name);
        }
        if (!StringUtils.isEmpty(code)) {
            wrapper.eq("role_code", code);
        }
        if (id != null) {
            wrapper.eq("role_id", id);
        }
        List<Role> list = this.list(wrapper);
        List<RoleDTO> listDto = new ArrayList<>();
        for (Role role : list) {
            RoleDTO roleDTO = new RoleDTO();
            BeanUtils.copyProperties(role, roleDTO);
            listDto.add(roleDTO);
        }
        return listDto;
    }
}
